document.addEventListener('DOMContentLoaded', function() {
    // 第一个轮播图逻辑（news-carousel）
   const newsCarousel = document.querySelector('.news-carousel');
   if (newsCarousel) {
       const mainSlide = newsCarousel.querySelector('img');
       const thumbnails = document.querySelectorAll('.thumbnail-list > div');
       
       if (mainSlide && thumbnails.length > 0) {
           const imageSequence = [
               'static/imgs/image1.jpg',
               'static/imgs/image2.jpg',
               'static/imgs/image3.jpg',
               'static/imgs/image4.jpg'
           ];
           
           let currentCarouselIndex = 0;
           let slideInterval;
           let isPaused = false;
   
           // 切换图片函数
           function switchToImage(index) {
               currentCarouselIndex = index;
               mainSlide.src = imageSequence[currentCarouselIndex];
               
               // 更新缩略图active状态
               thumbnails.forEach((thumb, i) => {
                   if(i === index) {
                       thumb.classList.add('active');
                   } else {
                       thumb.classList.remove('active');
                   }
               });
           }
   
           // 自动轮播函数
           function startAutoSlide() {
               if (slideInterval) clearInterval(slideInterval);
               slideInterval = setInterval(() => {
                   if (!isPaused) {
                       currentCarouselIndex = (currentCarouselIndex + 1) % imageSequence.length;
                       switchToImage(currentCarouselIndex);
                   }
               }, 3000);
           }
   
           // 暂停自动轮播
           function pauseAutoSlide() {
               isPaused = true;
           }
   
           // 恢复自动轮播
           function resumeAutoSlide() {
               isPaused = false;
               // 恢复时立即切换一次，避免等待间隔
               startAutoSlide();
           }
   
           // 缩略图交互
           thumbnails.forEach((thumb, index) => {
               // 鼠标悬停立即切换
               thumb.addEventListener('mouseenter', function() {
                   switchToImage(index);
                   pauseAutoSlide();
               });
               
               // 鼠标离开恢复自动轮播
               thumb.addEventListener('mouseleave', function() {
                   resumeAutoSlide();
               });
               
               // 点击也可以切换
               thumb.addEventListener('click', function(e) {
                   e.preventDefault();
                   switchToImage(index);
               });
           });
   
           // 整个轮播区域鼠标事件
           newsCarousel.addEventListener('mouseenter', pauseAutoSlide);
           newsCarousel.addEventListener('mouseleave', resumeAutoSlide);
   
           // 初始化
           switchToImage(0); // 默认显示第一张
           startAutoSlide();
       }
   }

    // 第二个轮播图逻辑（main-slide）
    const mainSlideContainer = document.querySelector('.main-slide');
    if (mainSlideContainer) {
        const dots = mainSlideContainer.querySelectorAll('.pagination-dots .dot');
        const mainImg = mainSlideContainer.querySelector('#main-slide-img');
        const slideTitle = mainSlideContainer.querySelector('.slide-title');
        
        if (dots.length > 0 && mainImg && slideTitle) {
            let currentSlideIndex = 0;
            let slideInterval;
            const slideDuration = 9000;

            function switchSlide(index) {
                dots.forEach(dot => dot.classList.remove('active'));
                dots[index].classList.add('active');
                mainImg.src = `static/imgs/${dots[index].getAttribute('data-img')}`;
                slideTitle.textContent = dots[index].getAttribute('data-title');
                currentSlideIndex = index;
            }

            function startAutoPlay() {
                if (slideInterval) clearInterval(slideInterval);
                slideInterval = setInterval(() => {
                    currentSlideIndex = (currentSlideIndex + 1) % dots.length;
                    switchSlide(currentSlideIndex);
                }, slideDuration);
            }

            dots.forEach((dot, index) => {
                dot.addEventListener('click', function(e) {
                    e.preventDefault();
                    clearInterval(slideInterval);
                    switchSlide(index);
                    startAutoPlay();
                });
            });

            mainSlideContainer.addEventListener('mouseenter', () => clearInterval(slideInterval));
            mainSlideContainer.addEventListener('mouseleave', startAutoPlay);

            startAutoPlay();
        }
    }

    // 其他功能（搜索框、快速链接等）
    const searchInput = document.querySelector('.hospital-search input');
    const searchBtn = document.querySelector('.hospital-search button');
    if (searchBtn && searchInput) {
        searchBtn.addEventListener('click', function() {
            if (searchInput.value.trim() !== '') {
                alert('搜索: ' + searchInput.value);
            }
        });
    }

    const quickLinks = document.querySelectorAll('.quick-link');
    quickLinks.forEach(link => {
        link.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-3px)';
        });
        link.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
        });
    });

    // 响应式导航菜单
    const navMenu = document.querySelector('.nav-menu');
    if (navMenu && window.innerWidth < 768) {
        navMenu.classList.add('mobile-menu');
    }
	
	
	const expertCarousel = document.querySelector('.expert-carousel-wrapper');
	    if (!expertCarousel) return;
	
	    const expertList = expertCarousel.querySelector('.expert-list');
	    const expertCards = expertList.querySelectorAll('.expert-card');
	    if (expertCards.length === 0) return;
	
	    // 1. 克隆足够多的卡片（至少3倍原始数量）
	    const cloneCount = expertCards.length * 4; // 克隆3倍数量确保足够滚动
	    for (let i = 0; i < cloneCount; i++) {
	        const clone = expertCards[i % expertCards.length].cloneNode(true);
	        expertList.appendChild(clone);
	    }
	
	    // 2. 计算卡片宽度（包括间距）
	    const cardStyle = window.getComputedStyle(expertCards[0]);
	    const cardWidth = expertCards[0].offsetWidth + parseInt(cardStyle.marginRight);
	
	    let currentPosition = 0;
	    let isPaused = false;
	    let animationId;
	    const scrollSpeed = 0.2; // 更慢的滚动速度
	
	    // 3. 无限滚动函数（永不跳回）
	    function infiniteScroll() {
	        if (!isPaused) {
	            currentPosition -= scrollSpeed;
	            
	            // 当滚动到足够远的位置时（超过原始卡片长度）
	            // 直接移除前面的卡片并追加到末尾
	            if (currentPosition <= -cardWidth * expertCards.length) {
	                const firstCard = expertList.firstElementChild;
	                if (firstCard) {
	                    expertList.removeChild(firstCard);
	                    const newCard = firstCard.cloneNode(true);
	                    expertList.appendChild(newCard);
	                    currentPosition += cardWidth;
	                }
	            }
	            
	            expertList.style.transform = `translateX(${currentPosition}px)`;
	        }
	        
	        animationId = requestAnimationFrame(infiniteScroll);
	    }
	
	    // 4. 鼠标交互控制（更平滑的暂停效果）
	    expertCarousel.addEventListener('mouseenter', () => {
	        isPaused = true;
	        expertList.style.transition = 'transform 0.3s ease-out';
	    });
	
	    expertCarousel.addEventListener('mouseleave', () => {
	        isPaused = false;
	        expertList.style.transition = 'none';
	    });
	
	    // 5. 初始化样式
	    expertList.style.transition = 'none';
	    expertList.style.willChange = 'transform';
	
	    // 6. 开始轮播
	    infiniteScroll();
	
	    // 7. 响应式处理
	    function handleResize() {
	        const newCardWidth = expertCards[0].offsetWidth + 
	                           parseInt(window.getComputedStyle(expertCards[0]).marginRight);
	        currentPosition = currentPosition * (newCardWidth / cardWidth);
	        expertList.style.transform = `translateX(${currentPosition}px)`;
	    }
	
	    window.addEventListener('resize', handleResize);
	
	    // 8. 清理
	    window.addEventListener('beforeunload', () => {
	        cancelAnimationFrame(animationId);
	    });
});